<template>
  <section class="todoapp">
    <TodoHeader @todaAdd="todaAdd"></TodoHeader>
    <TodoMinx
      :list="tainanleya"
      @todoDel="todoDel"
      @checkAll="checkAll"
    ></TodoMinx>
    <TodoFooter
      :type="type"
      :sum="sum"
      @screen="screen"
      @delAll="delAll"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue'
import TodoMinx from './components/TodoMinx.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  components: {
    TodoHeader,
    TodoMinx,
    TodoFooter,
  },
  data() {
    return {
      list: [
        {
          id: 1,
          value: '读万卷书',
          flag: false,
        },
        {
          id: 2,
          value: '读万卷书2',
          flag: true,
        },
        {
          id: 3,
          value: '读万卷书3',
          flag: false,
        },
      ],
      type: 'all',
    }
  },
  methods: {
    // 添加功能
    todaAdd(value) {
      if (value === '') return alert('大咩哟，打灭打咩')
      this.list = [
        ...this.list,
        {
          id: +new Date(),
          value: value,
          flag: false,
        },
      ]
    },

    // 删除功能
    todoDel(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },

    // 清除已完成
    delAll() {
      this.list = this.list.filter((item) => !item.flag)
    },

    // 切换
    screen(value) {
      this.type = value
    },

    // 全选
    checkAll() {
      this.list.forEach((item) => (item.flag = true))
    },
  },

  computed: {
    // 左下角剩余
    sum() {
      return this.list.filter((item) => !item.flag).length
    },

    tainanleya() {
      switch (this.type) {
        case 'all':
          return this.list
        case 'active':
          return this.list.filter((item) => !item.flag)
        case 'completed':
          return this.list.filter((item) => item.flag)
        default:
          return this.list
      }
    },
  },
}
</script>

<style></style>
